<template>
    <div class="login-container">
      <h1>登录</h1>
      <form @submit.prevent="login">
        <div>
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="credentials.username" required>
        </div>
        <div>
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="credentials.password" required>
        </div>
        <button type="submit" :disabled="confrimable">Login</button>
      </form>
    </div>
  </template>
   
  <script>
  export default {
    data() {
      return {
        credentials: {
          username: '',
          password: '',
          confrimable:false
        }
      };
    },
    methods: {
      login() {
        // 这里调用API进行登录操作
        this.confrimable = true
        
        this.$axios.post(this.$httpUrl+"/techuser/login",this.credentials).then(res=>{
            console.log(res.data);
            if(res.data.code==200){
                localStorage.setItem('CurUser',JSON.stringify(res.data.data))
                this.$router.replace( '/Main')
            }else{
                this.confrimable = false
                this.$message.error(
                    '登录失败，用户名或密码错误！'
                );
            }
       })

      }
    },
    beforeMount(){
        const curUser  = localStorage.getItem('CurUser');

        if(curUser!=null){
            console.log(curUser);
            
            this.$router.replace( '/Main')
        }
        console.log(curUser);
        
    }
  };
  </script>
   
  <style scoped>
  .login-container {
    width: 300px;
    margin: 100px auto;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  form div {
    margin: 10px 0;
  }
  label {
    display: block;
    margin-bottom: 5px;
  }
  input[type="text"], input[type="password"] {
    width: 93%;
    padding: 8px;
    
    margin-top: 2px;
  }
  button[type="submit"] {
    width: 100%;
    padding: 10px;
    margin-top: 10px;
    cursor: pointer;
  }
  </style>